package com.csii.activiti.server.sign.encrypt.utils;

import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import com.alibaba.fastjson.JSONObject;

import java.util.TreeMap;


public class SignEncryptUtils {

    public static String encrypt(Object data,String signKey){
        if (signKey == null){
            return null;
        }
        try {
            HMac mac = new HMac(HmacAlgorithm.HmacMD5, signKey.getBytes("utf-8"));
            //保证属性顺序一致性
            JSONObject orderJsonObject = new JSONObject(new TreeMap());
            orderJsonObject.putAll((JSONObject) JSONObject.toJSON(data));
            return mac.digestHex(orderJsonObject.toJSONString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
